﻿using EnterpriseWebsiteApi.ActionFilters;
using EnterpriseWebsiteApi.ActionResults;
using EnterpriseWebsiteApi.Dto;
using EnterpriseWebsiteApi.Dto.Article;
using EnterpriseWebsiteBLL;
using EnterpriseWebsiteUtil.Attributes;
using EnterpriseWebsiteUtil.Helper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EnterpriseWebsiteApi.Controllers.Web
{
    [Route("api/web/[controller]/[action]")]
    [ApiController]
    [ServiceFilter(typeof(LanguageHeaderFilter))]
    public class ArticleController : ControllerBase
    {
        private readonly ArticleService  _articleService;
        private readonly LangSplitHelper _langSplitHelper;
        private readonly Mapper _mapper;

        public ArticleController(ArticleService articleService, LangSplitHelper langSplitHelper, Mapper mapper)
        {
            _articleService = articleService;
            _langSplitHelper = langSplitHelper;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="language"></param>
        /// <param name="category"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetArticleList(LangType language,int category = 0,int pageIndex = 0)
        {
            try
            {
                TablePageDto<IDictionary<string,object>> tablePageDto = new TablePageDto<IDictionary<string, object>>();
                RefAsync<int> toalNumber = 0;
                var entities = await _articleService.GetAllArticlesAsync(toalNumber, pageIndex, 20, category,display:true);
                var dto = _mapper.MapList<ArticleWebListDTO>(entities);
                var lang = _langSplitHelper.ConvertToLang<ArticleWebListDTO>(dto,language) ;
                tablePageDto.Data = lang;
                tablePageDto.PageIndex = pageIndex;
                tablePageDto.PageSize = 20;
                tablePageDto.ToalNumber = toalNumber;
                return new Success(tablePageDto);
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }

        /// <summary>
        /// 获取文章
        /// </summary>
        /// <param name="id"></param>
        /// <param name="language"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetArticle(int id, LangType language)
        {
            try
            {
                var entity = await _articleService.GetArticleByIdAsync(id);
                if (entity == null)
                {
                    throw new Exception("找不到对应文章，请检查参数");
                }
                var dto = _mapper.Map<ArticleWebSingleDTO>(entity);
                var lang = _langSplitHelper.ConvertToLang(dto,language);
                return new Success(lang);
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }
    }
}
